﻿using System;
using System.IO;
using System.Text;

namespace SharpCompress.Test.Xz;

public abstract class XzTestsBase : IDisposable
{
    public XzTestsBase()
    {
        RewindEmpty(CompressedEmptyStream);
        Rewind(CompressedStream);
        RewindIndexed(CompressedIndexedStream);
    }

    public void Dispose()
    {
        CompressedEmptyStream?.Dispose();
        CompressedStream?.Dispose();
        CompressedIndexedStream?.Dispose();
    }

    protected virtual void RewindEmpty(Stream stream) => stream.Position = 0;

    protected virtual void Rewind(Stream stream) => stream.Position = 0;

    protected virtual void RewindIndexed(Stream stream) => stream.Position = 0;

    protected Stream CompressedEmptyStream { get; } = new MemoryStream(CompressedEmpty);

    protected static byte[] CompressedEmpty { get; } =
        [
            0xfd,
            0x37,
            0x7a,
            0x58,
            0x5a,
            0x00,
            0x00,
            0x01,
            0x69,
            0x22,
            0xde,
            0x36,
            0x00,
            0x00,
            0x00,
            0x00,
            0x1c,
            0xdf,
            0x44,
            0x21,
            0x90,
            0x42,
            0x99,
            0x0d,
            0x01,
            0x00,
            0x00,
            0x00,
            0x00,
            0x01,
            0x59,
            0x5a,
        ];

    protected static byte[] OriginalEmptyBytes => Encoding.ASCII.GetBytes(OriginalEmpty);

    protected static string OriginalEmpty { get; } = string.Empty;

    protected Stream CompressedStream { get; } = new MemoryStream(Compressed);

    protected static byte[] Compressed { get; } =
        [
            0xfd,
            0x37,
            0x7a,
            0x58,
            0x5a,
            0x00,
            0x00,
            0x04,
            0xe6,
            0xd6,
            0xb4,
            0x46,
            0x02,
            0x00,
            0x21,
            0x01,
            0x16,
            0x00,
            0x00,
            0x00,
            0x74,
            0x2f,
            0xe5,
            0xa3,
            0xe0,
            0x01,
            0xe4,
            0x01,
            0x3c,
            0x5d,
            0x00,
            0x26,
            0x98,
            0x4a,
            0x47,
            0xc6,
            0x6a,
            0x27,
            0xd7,
            0x36,
            0x7a,
            0x05,
            0xb9,
            0x4f,
            0xd7,
            0xde,
            0x52,
            0x4c,
            0xca,
            0x26,
            0x4f,
            0x23,
            0x60,
            0x4d,
            0xf3,
            0x1f,
            0xa3,
            0x67,
            0x49,
            0x53,
            0xd0,
            0xf5,
            0xc7,
            0xa9,
            0x3e,
            0xd6,
            0xb5,
            0x3d,
            0x2b,
            0x02,
            0xbe,
            0x83,
            0x27,
            0xe2,
            0xa6,
            0xc3,
            0x13,
            0x4a,
            0x31,
            0x14,
            0x33,
            0xed,
            0x9a,
            0x85,
            0x1d,
            0x05,
            0x6e,
            0x7e,
            0xa4,
            0x91,
            0xbf,
            0x46,
            0x71,
            0x7d,
            0xa7,
            0xfb,
            0x12,
            0x10,
            0xdf,
            0x21,
            0x73,
            0x75,
            0xd8,
            0xd9,
            0xab,
            0x8f,
            0x1f,
            0x8b,
            0xb0,
            0xb9,
            0x3f,
            0x9a,
            0xa5,
            0x1e,
            0xd4,
            0x2f,
            0xdf,
            0x09,
            0xb3,
            0xfe,
            0x45,
            0xef,
            0x16,
            0xec,
            0x95,
            0x68,
            0x64,
            0xbb,
            0x42,
            0x0c,
            0x8b,
            0x96,
            0x27,
            0x30,
            0x62,
            0x42,
            0x91,
            0x7c,
            0xf3,
            0x6e,
            0x4d,
            0x03,
            0xc5,
            0x00,
            0x04,
            0x73,
            0xdd,
            0xee,
            0xb0,
            0xaa,
            0xd6,
            0x0b,
            0x11,
            0x90,
            0x81,
            0xd4,
            0xaa,
            0x69,
            0x63,
            0xfa,
            0x2f,
            0xb4,
            0x25,
            0x0a,
            0x7f,
            0xf9,
            0x47,
            0x77,
            0xb1,
            0x1f,
            0xc3,
            0xb4,
            0x4d,
            0x51,
            0xf8,
            0x23,
            0x3a,
            0x7c,
            0x44,
            0xc8,
            0xcc,
            0xca,
            0x72,
            0x09,
            0xae,
            0xc9,
            0x7b,
            0x7e,
            0x91,
            0x5d,
            0xff,
            0xc4,
            0xeb,
            0xfd,
            0xa1,
            0x9b,
            0xd4,
            0x8d,
            0xd7,
            0xd3,
            0x57,
            0xac,
            0x7e,
            0x3b,
            0x97,
            0x2e,
            0xe4,
            0xc2,
            0x2e,
            0x93,
            0x3d,
            0xb0,
            0x16,
            0x64,
            0x78,
            0x45,
            0xb1,
            0xc9,
            0x40,
            0x96,
            0xcf,
            0x5b,
            0xc2,
            0x2f,
            0xaa,
            0xba,
            0xcf,
            0x98,
            0x38,
            0x21,
            0x3d,
            0x1a,
            0x13,
            0xe8,
            0xa6,
            0xa6,
            0xdf,
            0xf4,
            0x3d,
            0x01,
            0xa1,
            0x9d,
            0xc1,
            0x3e,
            0x37,
            0xac,
            0x20,
            0xc4,
            0xef,
            0x18,
            0xb1,
            0xeb,
            0x35,
            0xf4,
            0x66,
            0x9a,
            0x47,
            0x3c,
            0xce,
            0x7c,
            0xad,
            0xdb,
            0x2e,
            0x39,
            0xf5,
            0x8d,
            0x4a,
            0x1d,
            0x65,
            0xc2,
            0x0f,
            0xa4,
            0x40,
            0x7e,
            0xe6,
            0xa7,
            0x17,
            0xce,
            0x75,
            0x7f,
            0xd9,
            0xa3,
            0xf9,
            0x27,
            0x42,
            0xd7,
            0x98,
            0x54,
            0x17,
            0xa7,
            0x7a,
            0x7c,
            0x82,
            0xdf,
            0xeb,
            0x08,
            0x28,
            0x86,
            0xdd,
            0x57,
            0x77,
            0x92,
            0x80,
            0x5f,
            0x7b,
            0x3b,
            0xce,
            0x77,
            0x72,
            0xff,
            0xa3,
            0x85,
            0xd8,
            0x5c,
            0x8a,
            0xb7,
            0x83,
            0x58,
            0xfa,
            0xbd,
            0x72,
            0xe3,
            0x66,
            0x9d,
            0x3b,
            0xff,
            0x13,
            0x5b,
            0x0b,
            0xf1,
            0x6c,
            0xa6,
            0xb1,
            0x3b,
            0x85,
            0x3b,
            0x47,
            0x91,
            0xc8,
            0x7c,
            0x38,
            0xe2,
            0xe5,
            0x54,
            0xf8,
            0x27,
            0xee,
            0x00,
            0xff,
            0xd3,
            0x68,
            0xf1,
            0xc6,
            0xc7,
            0xd7,
            0x24,
            0x00,
            0x01,
            0xd8,
            0x02,
            0xe5,
            0x03,
            0x00,
            0x00,
            0xac,
            0x16,
            0x1f,
            0xa4,
            0xb1,
            0xc4,
            0x67,
            0xfb,
            0x02,
            0x00,
            0x00,
            0x00,
            0x00,
            0x04,
            0x59,
            0x5a,
        ];
    protected static byte[] OriginalBytes => Encoding.ASCII.GetBytes(Original);

    protected static string Original { get; } =
        "Mary had a little lamb,\r\n"
        + "His fleece was white as snow,\r\n"
        + "And everywhere that Mary went,\r\n"
        + "The lamb was sure to go.\r\n"
        + "\r\n"
        + "He followed her to school one day,\r\n"
        + "Which was against the rule,\r\n"
        + "It made the children laugh and play\r\n"
        + "To see a lamb at school.\r\n"
        + "\r\n"
        + "And so the teacher turned it out,\r\n"
        + "But still it lingered near,\r\n"
        + "And waited patiently about,\r\n"
        + "Till Mary did appear.\r\n"
        + "\r\n"
        + "\"Why does the lamb love Mary so?\"\r\n"
        + "The eager children cry.\r\n"
        + "\"Why, Mary loves the lamb, you know.\"\r\n"
        + "The teacher did reply.";

    protected Stream CompressedIndexedStream { get; } = new MemoryStream(CompressedIndexed);

    protected static byte[] CompressedIndexed { get; } =
        [
            0xfd,
            0x37,
            0x7a,
            0x58,
            0x5a,
            0x00,
            0x00,
            0x01,
            0x69,
            0x22,
            0xde,
            0x36,
            0x03,
            0xc0,
            0xe3,
            0x02,
            0x80,
            0x20,
            0x21,
            0x01,
            0x00,
            0x00,
            0x00,
            0x00,
            0x7e,
            0xe5,
            0xd7,
            0x32,
            0xe0,
            0x0f,
            0xff,
            0x01,
            0x5b,
            0x5d,
            0x00,
            0x26,
            0x98,
            0x4a,
            0x47,
            0xc6,
            0x6a,
            0x27,
            0xd7,
            0x36,
            0x7a,
            0x05,
            0xb9,
            0x4f,
            0xd7,
            0xde,
            0x3a,
            0x0e,
            0xee,
            0x1b,
            0xd7,
            0x81,
            0xe9,
            0xf5,
            0x90,
            0x1e,
            0xd5,
            0x9e,
            0x88,
            0x32,
            0x1c,
            0x7b,
            0x43,
            0x84,
            0x02,
            0x58,
            0x92,
            0xcf,
            0x97,
            0xfc,
            0xae,
            0x01,
            0x83,
            0x23,
            0x48,
            0x93,
            0xc6,
            0x56,
            0xcc,
            0x6d,
            0xb1,
            0x23,
            0x10,
            0x24,
            0x3b,
            0x9e,
            0x06,
            0xaa,
            0xc0,
            0xce,
            0x86,
            0x0a,
            0xb7,
            0x9f,
            0x99,
            0x61,
            0xbe,
            0x3b,
            0x6d,
            0xfe,
            0x60,
            0xef,
            0x14,
            0x35,
            0x7f,
            0x21,
            0xe8,
            0x96,
            0x0e,
            0xbd,
            0x41,
            0x7c,
            0x65,
            0x89,
            0x96,
            0x28,
            0x5e,
            0x85,
            0xa6,
            0x4b,
            0xf3,
            0xf9,
            0xf8,
            0x25,
            0x31,
            0x4a,
            0xbb,
            0x72,
            0xce,
            0xcf,
            0x53,
            0xdf,
            0x13,
            0x42,
            0x2d,
            0xbc,
            0x95,
            0xa5,
            0x6d,
            0xc4,
            0x8c,
            0x72,
            0x99,
            0xe8,
            0x9a,
            0xcf,
            0x80,
            0xd4,
            0xc4,
            0x3f,
            0x55,
            0xc3,
            0x9b,
            0x00,
            0xce,
            0x65,
            0x27,
            0x6e,
            0xbf,
            0xb2,
            0x88,
            0xab,
            0xc0,
            0x5f,
            0xf9,
            0xd0,
            0xc8,
            0xbb,
            0xd7,
            0x48,
            0xd7,
            0x2e,
            0x5e,
            0xbb,
            0x23,
            0x35,
            0x6e,
            0x62,
            0xb6,
            0x13,
            0xd4,
            0x06,
            0xd1,
            0x5b,
            0x97,
            0xee,
            0x5b,
            0x89,
            0x78,
            0x07,
            0x24,
            0x74,
            0x59,
            0x06,
            0x1e,
            0x7f,
            0x8c,
            0xb0,
            0x48,
            0xff,
            0x0a,
            0x76,
            0xb2,
            0x07,
            0xa0,
            0x99,
            0xf5,
            0x4b,
            0x68,
            0xd4,
            0x55,
            0x32,
            0xb3,
            0x17,
            0x7b,
            0xb6,
            0x26,
            0xdb,
            0x1c,
            0xc3,
            0x0b,
            0xda,
            0x3e,
            0x46,
            0xba,
            0x1a,
            0x67,
            0x23,
            0xb7,
            0x2a,
            0x40,
            0xdc,
            0xc9,
            0xa2,
            0xe4,
            0xb5,
            0x68,
            0x5c,
            0x81,
            0x60,
            0xa7,
            0xad,
            0xe6,
            0xba,
            0xbb,
            0x0d,
            0x82,
            0x8a,
            0xe2,
            0x03,
            0xa9,
            0x22,
            0x09,
            0x5e,
            0xd8,
            0x69,
            0xfa,
            0x29,
            0xd1,
            0x32,
            0xa1,
            0xf0,
            0x9b,
            0x3c,
            0xc3,
            0x0b,
            0x9a,
            0x53,
            0xf0,
            0x3e,
            0xf3,
            0x1b,
            0x77,
            0xee,
            0x8f,
            0xa6,
            0x15,
            0x02,
            0x77,
            0x14,
            0x54,
            0x60,
            0xae,
            0xbe,
            0x91,
            0x9e,
            0xe6,
            0x8b,
            0x87,
            0x6e,
            0x46,
            0x44,
            0x64,
            0xc7,
            0x58,
            0x90,
            0x62,
            0x25,
            0x32,
            0xf9,
            0xcd,
            0xd2,
            0x73,
            0x2e,
            0x3f,
            0xd7,
            0x5d,
            0x3c,
            0x86,
            0x1c,
            0xa8,
            0x35,
            0xa9,
            0xc2,
            0xcb,
            0x59,
            0xcb,
            0xac,
            0xb3,
            0x03,
            0x12,
            0xd4,
            0x8a,
            0xde,
            0xd5,
            0xc1,
            0xd8,
            0x0c,
            0x32,
            0x49,
            0x87,
            0x97,
            0x62,
            0x4f,
            0x32,
            0x39,
            0x63,
            0x5b,
            0x8b,
            0xd1,
            0x6c,
            0x5c,
            0x90,
            0xd9,
            0x93,
            0x13,
            0xae,
            0x70,
            0xf5,
            0x2f,
            0x40,
            0xaf,
            0x01,
            0x95,
            0x01,
            0x0c,
            0xc5,
            0xfa,
            0x82,
            0xf8,
            0x71,
            0x9d,
            0x53,
            0xe6,
            0x47,
            0x6e,
            0x99,
            0x54,
            0x57,
            0x41,
            0x72,
            0xea,
            0xf5,
            0x78,
            0xdd,
            0x86,
            0xbd,
            0x00,
            0x00,
            0x00,
            0x72,
            0x6a,
            0xf2,
            0x47,
            0x03,
            0xc0,
            0xcb,
            0x01,
            0x8d,
            0x02,
            0x21,
            0x01,
            0x00,
            0x00,
            0x00,
            0x00,
            0xfb,
            0xa7,
            0xf7,
            0x94,
            0xe0,
            0x01,
            0x0c,
            0x00,
            0xc3,
            0x5d,
            0x00,
            0x06,
            0x82,
            0xca,
            0x9b,
            0x77,
            0x93,
            0x57,
            0xb3,
            0x76,
            0xbd,
            0x8b,
            0xcb,
            0xee,
            0xf4,
            0x2c,
            0xff,
            0x7f,
            0x95,
            0x33,
            0x15,
            0x10,
            0xa5,
            0xf9,
            0xfd,
            0xa6,
            0xbb,
            0x9e,
            0xf9,
            0x75,
            0x67,
            0xee,
            0xec,
            0x8b,
            0x40,
            0xea,
            0x32,
            0x47,
            0x3d,
            0x26,
            0xbe,
            0x11,
            0x9c,
            0xa6,
            0x40,
            0xbe,
            0x84,
            0x1f,
            0x1b,
            0x35,
            0x1a,
            0x66,
            0x10,
            0x9c,
            0xf4,
            0x12,
            0x1a,
            0x95,
            0x81,
            0xb5,
            0x55,
            0x6b,
            0xc5,
            0x42,
            0xfd,
            0x37,
            0x70,
            0xc5,
            0x08,
            0xa4,
            0x27,
            0x67,
            0x11,
            0x0b,
            0x1f,
            0xcc,
            0xdb,
            0x54,
            0x9b,
            0x5a,
            0x5f,
            0xee,
            0x21,
            0x63,
            0xdd,
            0x4b,
            0xbc,
            0x49,
            0x95,
            0x6d,
            0xf4,
            0xcb,
            0x9a,
            0x9a,
            0x5e,
            0xe4,
            0x7d,
            0x0f,
            0x02,
            0x22,
            0xa9,
            0x42,
            0x46,
            0x1a,
            0x04,
            0x87,
            0x43,
            0x72,
            0x59,
            0xa4,
            0xd6,
            0xeb,
            0x69,
            0x36,
            0xde,
            0xea,
            0x53,
            0x8c,
            0x89,
            0xd7,
            0x22,
            0xa6,
            0xf7,
            0xa8,
            0x4c,
            0x72,
            0x6c,
            0x80,
            0x69,
            0x01,
            0xb2,
            0xa7,
            0xe8,
            0x8b,
            0x94,
            0xaf,
            0x0e,
            0x47,
            0x58,
            0x1d,
            0x0e,
            0x5c,
            0x7c,
            0x33,
            0x9f,
            0x21,
            0x17,
            0x2c,
            0x4f,
            0x3d,
            0x72,
            0xff,
            0xcf,
            0x7a,
            0x4f,
            0x82,
            0x5b,
            0x85,
            0x28,
            0x70,
            0xf4,
            0x8c,
            0x81,
            0x41,
            0xb8,
            0x20,
            0x5c,
            0x3e,
            0x02,
            0x5e,
            0x5a,
            0x61,
            0xbb,
            0x2f,
            0x64,
            0xc5,
            0x4e,
            0x53,
            0xe4,
            0xca,
            0xe4,
            0xd9,
            0x75,
            0xaf,
            0x15,
            0x4d,
            0xff,
            0x01,
            0xec,
            0x13,
            0x4a,
            0x70,
            0x00,
            0x04,
            0xf9,
            0xfa,
            0x00,
            0x00,
            0x99,
            0x57,
            0xc4,
            0x96,
            0x00,
            0x02,
            0xf7,
            0x02,
            0x80,
            0x20,
            0xdf,
            0x01,
            0x8d,
            0x02,
            0x00,
            0x00,
            0x4c,
            0x41,
            0xe6,
            0xa1,
            0x9b,
            0xe3,
            0x51,
            0x40,
            0x03,
            0x00,
            0x00,
            0x00,
            0x00,
            0x01,
            0x59,
            0x5a,
        ];

    protected static byte[] OriginalIndexedBytes => Encoding.ASCII.GetBytes(OriginalIndexed);

    protected static string OriginalIndexed { get; } =
        Original
        + Original
        + Original
        + Original
        + Original
        + Original
        + Original
        + Original
        + Original;
}
